package net.piggy.test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FastFileReadWrite {
    public static void main(String[] args) {

        try (FileInputStream fis = new FileInputStream("D:\\software\\BaiduNetdisk_7.26.0.10.exe");
             FileOutputStream fos = new FileOutputStream("D:\\software\\Baidu_7.26.0.10.exe");
             FileChannel inputChannel = fis.getChannel();
             FileChannel outputChannel = fos.getChannel()) {

            ByteBuffer buffer = ByteBuffer.allocateDirect(40960);
            while (inputChannel.read(buffer) != -1) {
                buffer.flip(); // 切换为读模式
                while (buffer.hasRemaining()) {
                    outputChannel.write(buffer);
                }
                buffer.clear(); // 清空缓冲区
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}